﻿#include"XQLoginWidget.h"
#include"XQUserInfo.h"
#include"XQAppInfo.h"
#include<QComboBox>
#include<QLineEdit>
#include<QAction>
#include<QCheckBox>
#include<QLabel>
#define PortraitRole Qt::UserRole 
#define PasswordRole (Qt::UserRole+1)
#define PasswordStateRole (Qt::UserRole+2)
XQLoginWidget::XQLoginWidget(QWidget* parent, bool AutoUi)
	:XQWidget(parent)
{
	if(AutoUi)
		init();
}

XQLoginWidget::~XQLoginWidget()
{
	/*if (m_userNameAction != nullptr)
		m_userNameAction->deleteLater();
	if (m_passwordAction != nullptr)
		m_passwordAction->deleteLater();*/
}

QPixmap XQLoginWidget::portrait() const
{
	return m_portrait;
}

QString XQLoginWidget::currentUserName()const
{
	return m_userNameIn->currentText();
}

QStringList XQLoginWidget::userNames()const
{
	QStringList list;
	for (int i = 0; i < m_userNameIn->count(); ++i) {
		list<< m_userNameIn->itemText(i);
	}
	return list;
	//return m_users.keys();
}

XQLoginWidget::User XQLoginWidget::user(const QString& userName)const
{
	auto index = m_userNameIn->findText(userName);
	if(index==-1)
		return User();
	return this->user(index);
}

XQLoginWidget::User XQLoginWidget::user(int index) const
{
	if (index<0|| index>=m_userNameIn->count())
		return User();
	User user;
	user.userName = m_userNameIn->itemText(index);
	user.portrait = m_userNameIn->itemData(index, PortraitRole).value<QPixmap>();
	user.password = m_userNameIn->itemData(index, PasswordRole).toString();
	user.savePasswordState = m_userNameIn->itemData(index, PasswordStateRole).toBool();
	return user;
}

QList<XQLoginWidget::User> XQLoginWidget::users() const
{
	QList<User> list;
	for (int index = 0; index < m_userNameIn->count(); ++index) {
		list << this->user(index);
	}
	return list;
}

QString XQLoginWidget::password()const
{
	return std::move(m_passwordIn->text());
}

bool XQLoginWidget::autoLoginState() const
{
	return m_autoLoginBtn->isChecked();
}
bool XQLoginWidget::savePasswordState() const
{
	return m_savePasswordBtn->isChecked();
}
void XQLoginWidget::setCurrentUserName(const QString& name)
{
	m_userNameIn->setCurrentText(name);
}

void XQLoginWidget::addUser(const QPixmap& portrait, const QString& userName, const QString& password, bool savePassword)
{
	auto user = m_userNameIn;
	if (userName.isEmpty()||user==nullptr)
		return;
	int index = user->findText(userName);
	if(index==-1)
	{
		user->addItem(userName);
		index = user->findText(userName);
	}
	//user->setItemIcon(index, portrait);
	user->setItemData(index, portrait, PortraitRole);
	user->setItemData(index, password,PasswordRole);
	user->setItemData(index, savePassword, PasswordStateRole);
}
void XQLoginWidget::addUser(const QString& userName, const QString& password, bool savePassword)
{
	addUser(QPixmap(),userName,password,savePassword);
}
void XQLoginWidget::remove(const QString& userName)
{
	int index=m_userNameIn->findText(userName);
	if(index!=-1)
		m_userNameIn->removeItem(index);
}

void XQLoginWidget::clearUsers()
{
	m_userNameIn->clear();
}

void XQLoginWidget::setPassword(const QString& password)
{
	//qInfo() << password;
	m_passwordIn->setText(password);
}

void XQLoginWidget::setPortrait(const QPixmap& image, bool save)
{
	if(save)
		m_portrait = image;
	auto pixmap = QPixmap(image);
	if (pixmap.isNull())
		pixmap = QPixmap(":/XQLoginWidget/images/portrait/default.jpg");//为空时的默认值
	pixmap = pixmap.scaled(m_portraitLabel->size());
	pixmap.setMask(QBitmap::fromImage(QPixmap(":/XQLoginWidget/images/portrait/mask.png").scaled(m_portraitLabel->size()).toImage()));
	m_portraitLabel->setPixmap(pixmap);
	m_portraitLabel->update();
}

void XQLoginWidget::setAutoLoginState(bool login)
{
	m_autoLoginBtn->setChecked(login);
}

void XQLoginWidget::setSavePasswordState(bool save)
{
	m_savePasswordBtn->setChecked(save);
}
void XQLoginWidget::userIndexChanged(int index)
{
	auto user = this->user(index);
	if (user.savePasswordState)
		m_passwordIn->setText(user.password);
	else
		m_passwordIn->setText("");
	setSavePasswordState(user.savePasswordState);
	setPortrait(user.portrait);
}